package com.kj.tms.common.bean.session;


import com.kj.tms.common.bean.exception.SessionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * httpsession实现类
 */
@Service
public class HttpSessionImpl implements SessionHandle {

    @Autowired
    private HttpServletRequest request;


    /**
     * @see SessionHandle#getId()
     */
    @Override
    public String getId() {
        return request.getSession().getId();
    }

    /**
     * @see SessionHandle#setAttribute(String, Object)
     */
    @Override
    public void setAttribute(String name, Object value) throws SessionException {
        //request.getSession().setMaxInactiveInterval(10);
        request.getSession().setAttribute(name, value);
    }

    /**
     * @see SessionHandle#getAttribute(String)
     */
    @Override
    public Object getAttribute(String name) throws SessionException {
        return request.getSession().getAttribute(name);
    }

    /**
     * @see SessionHandle#removeAttribute(String)
     */
    @Override
    public void removeAttribute(String name) throws SessionException {
        request.getSession().removeAttribute(name);
    }

    /**
     * @see SessionHandle#getAttributeNames()
     */
    @Override
    public Enumeration<?> getAttributeNames() throws SessionException {
        return request.getSession().getAttributeNames();
    }

    /**
     * @see SessionHandle#invalidate()
     */
    @Override
    public void invalidate() throws SessionException {
        request.getSession().invalidate();
    }

    /**
     * @see SessionHandle#expire()
     */
    @Override
    public void expire() {

    }
}
